﻿using System;
using System.Web.UI.WebControls;
using MaLu.Common;
using MaLu.Data;
using System.Drawing;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using MaLu.BusiLogic;

namespace MaluJg.web.Poverty
{
    public partial class Index : ProvertyPage
    {
      
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                tbYear.Text = Year.ToString();
                ddlVillage.DataSource = SqlHelper.Instance.ExecuteDataTable(Provider, "select * from pDicVillages");
                ddlVillage.DataTextField = "name";
                ddlVillage.DataValueField = "id";
                ddlVillage.DataBind();

                ListItem li = ddlVillage.Items.FindByText(Village);
                if (li != null)
                    ddlVillage.SelectedValue = li.Value;

                ddlVillage.Items.Insert(0, new ListItem("全部", "0"));

                if (IsProvertySubAdmin)
                {
                    ddlVillage.Enabled = false; 
                }
                CreateChart();
            }
        }
        public Color GetRandomColor(int seed)
        {
            Random RandomNum_First = new Random(seed);
            Random RandomNum_Sencond = new Random(seed * seed);

            int int_Red = RandomNum_First.Next(256);
            int int_Green = RandomNum_Sencond.Next(256);
            int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
            int_Blue = (int_Blue > 255) ? 255 : int_Blue;

            return Color.FromArgb(int_Red, int_Green, int_Blue);
        }
        private void CreateChart()
        {
            chart.Titles[0].Text = Year + "年马陆镇扶贫帮困资金";
            DataSet ds = BLPoverty.GetAllowanceByYearAndVillage(Provider, Year, Village);
            Series s = new Series();
            s.ChartType = SeriesChartType.Column;
            s.XValueType = ChartValueType.String;
            s.CustomProperties = "DrawingStyle=Emboss";
            DataTable data = ds.Tables[0];
            float total = 0.0f;
            foreach (DataColumn col in data.Columns)
            {

                foreach (DataRow dr in data.Rows)
                {
                    float temp = 0;
                    float.TryParse(dr[col.ColumnName].ToString(), out temp);
                    total += temp;
                }
            }
            foreach (DataColumn col in data.Columns)
            {
                float sum = 0.0f;
                foreach (DataRow dr in data.Rows)
                {
                    float temp = 0;
                    float.TryParse(dr[col.ColumnName].ToString(), out temp);
                    sum += temp;
                }

                DataPoint dp = new DataPoint();
                dp.Label = "0%";
                if (total != 0.0f)
                {
                    dp.Label = (sum / total * 100).ToString("f2") + "%";
                }
                dp.Color = GetRandomColor(Convert.ToInt32(sum));
                dp.YValues = new double[] { Convert.ToDouble(sum) };
                dp.LegendText = BLPoverty.GetName(col.ColumnName);
                dp.AxisLabel = BLPoverty.GetName(col.ColumnName);
                s.Points.Add(dp);
            }
            chart.Series.Add(s);

            string sum1 = string.Empty;
            DataTable dtsum = ds.Tables[1];
            if (dtsum.Rows.Count > 0)
                sum1 ="共"+ dtsum.Rows[0][0].ToString() + "元";
            chart.Titles[0].Text = Year + "年马陆镇扶贫帮困资金                         " + sum1;
        }
    }
}
